int g_iStep = 0;
int g_iAnim = 0;

int g_iTimeStep = 16;
int g_iMaxStep = 1;

void setMatrixAnimation(int anim)
{
  g_iAnim = anim%6;
  g_iStep = 0;

  switch(g_iAnim)
  {
  case LED_ANIM_BLANK:
  default:
    {
      g_iTimeStep = 500; 
      g_iMaxStep = 1; 
    }
    break;
  case LED_ANIM_ERROR:
    {
      g_iTimeStep = 125; 
      g_iMaxStep = 4;
    }
    break;
  case LED_ANIM_WARNING:
    {
      g_iTimeStep = 250; 
      g_iMaxStep = 6; 
    }
    break;
  case LED_ANIM_CYLON:
    {
      g_iTimeStep = 64; 
      g_iMaxStep = 16; 
    }
    break;
  case LED_ANIM_GREENWHEEL:
    {
      g_iTimeStep = 64; 
      g_iMaxStep = 64; 
    }
    break;
  case LED_ANIM_ECHO:
    {
      g_iTimeStep = 64; 
      g_iMaxStep = 16; 
    }
    break;
  }
}

void drawMatrix8x8(void)
{
  static unsigned long g_ulHookTime=0;
  static unsigned long g_ulAnimTime=0;

  unsigned long ulTimeSlice = 0,  ulCurrentTime=millis();

  if(g_ulHookTime==0) g_ulHookTime = ulCurrentTime;

  ulTimeSlice = ulCurrentTime-g_ulHookTime;
  g_ulHookTime = ulCurrentTime;

  g_ulAnimTime += ulTimeSlice;
  if(g_ulAnimTime>=g_iTimeStep)
  {
    int x, y, z;

    g_ulAnimTime = g_ulAnimTime - g_iTimeStep;

    if(g_iStep == 0)
    {
      Rb.blankDisplay();
    }

    if(g_iStep>g_iMaxStep)
    {
      g_iStep = 0;
    }

    switch(g_iAnim)
    {
    case LED_ANIM_BLANK:
    default: // BLANK
      {
        // nothing !
        if(g_iStep==0) Rb.fill(BLACK);
      }
      break;
    case LED_ANIM_ERROR:
      {
        if(g_iStep%4==1)
          Rb.fill(Rb.makeColor(255, 0, 0));
        else if(g_iStep%4==0)
          Rb.fill(Rb.makeColor(0, 0, 0));
        else
          Rb.fill(Rb.makeColor(175, 0, 0));
      }
      break;
    case LED_ANIM_WARNING:
      {
        if(g_iStep==0)
          Rb.fillRectangle(0, 0, 8, 8, Rb.makeColor(128, 128, 128));
        else if(g_iStep==1)
          Rb.fillRectangle(1, 1, 6, 6, YELLOW);
        else if(g_iStep==2)
          Rb.fillRectangle(0, 0, 8, 8, Rb.makeColor(128, 128, 128));
        else if(g_iStep==3)
          Rb.fillRectangle(2, 2, 4, 4, YELLOW);
        else if(g_iStep==4)
          Rb.fillRectangle(0, 0, 8, 8, Rb.makeColor(128, 128, 128));
        else if(g_iStep==5)
          Rb.fillRectangle(3, 3, 2, 2, YELLOW);
      }
      break;
    case LED_ANIM_CYLON: // CYLON
      {
        if(g_iStep>8) x=16-g_iStep;
        else x=g_iStep;

        Rb.fill(BLACK);

        Rb.drawVerticalLine(x-2, 0, 8, Rb.makeColor(31, 0, 0));
        Rb.drawVerticalLine(x-1, 0, 8, Rb.makeColor(95, 0, 0));
        Rb.drawVerticalLine(x  , 0, 8, Rb.makeColor(175, 0, 0));
        Rb.drawVerticalLine(x+1,0, 8, Rb.makeColor(175, 0, 0));
        Rb.drawVerticalLine(x+2,0, 8, Rb.makeColor(31, 0, 0));
      }
      break;
    case LED_ANIM_GREENWHEEL: // GREEN WHEEL
      {
        x=g_iStep/8;

        if(g_iStep == 0)
          Rb.fill(BLACK);

        Rb.drawLine(x,0, 7-x,7, GREEN);
        Rb.drawLine(0,7-x, 7,x, GREEN);
      }
      break;
    case LED_ANIM_ECHO: // ECHO
      {
        z=g_iStep;

        static int c=-1;
        if(g_iStep == 0) c = (c+1)%4;

        if(c == 0) {
          x=3; 
          y=3;
        }
        else if(c == 1) {
          x=4; 
          y=3;
        }
        else if(c == 2) {
          x=4; 
          y=4;
        }
        else if(c == 3) {
          x=3; 
          y=4;
        }

        Rb.fill(BLACK);

        if(z>4) Rb.drawCircle(x, y, z-4, Rb.makeColor(12, 0, 12));
        if(z>3) Rb.drawCircle(x, y, z-3, Rb.makeColor(62, 0, 62));
        if(z>2) Rb.drawCircle(x, y, z-2, Rb.makeColor(112, 0, 112));        
        if(z>1) Rb.drawCircle(x, y, z-1, Rb.makeColor(180, 0, 180));
        Rb.drawCircle(x, y, z, Rb.makeColor(255, 0, 255));
      }
      break;
    }

    g_iStep++;
  }
}









